﻿#pragma once
#include <QString>
#include <QList>
#include "Configer.h"
#include "AoiReq.h"
#include "AoiDetPlusRsp.h"

typedef struct _QueryAiUrlReq
{
	QString ProductName;
	QString SceneName;
} QueryAiUrlReq;

typedef struct _QueryAiUrlRes
{
	int errorCode;
	QString descMsg;
	QString ProjectName;
	QString TaskName;
	QString ProjectID;
	QString ConnectService;
	QString ProjectStatus;
} QueryAiUrlRes;

typedef struct _QueryProductListResult
{
	int errorCode;
	QString descMsg;
	QString productNum;
	QList<QString> ProductList;
} QueryProductListResult;

enum HttpRequestType
{
	POST = 11,
	GET,
	DEL,
	PUT
};

class HttpStatusInfo
{
public:
	HttpStatusInfo(int httpCode, QString reason);
	static HttpStatusInfo getByCode(int httpCode);
	int httpCode;
	QString reason;
};

class HttpUtils
{
private:
	bool checkReqParams();
	QString m_Scene;
	QString getScene();
	int sendInferRequestCommon(QString url, QString reqJson, QString& rspJson);
	int sendInferRequestCommon(QString url, QString scene, QString reqJson, QString& rspJson);
	int sendInferReq(QString url, aoi::SceneTag sceneTag, aoi::AoiReq& req, aoi::DetPlusRsp& detPlusRsp, QString &rspJson);
public:
	QString m_Token;
	QString m_HostUrl;
	QString m_SerialNumber;

	SSLParamer m_SslParam;
	HttpUtils();
	~HttpUtils();
	void setScene(QString sceneName);
	static QString getTokenOrDefault(QString currentToken);
	void setToken(QString token);
	QString getToken();
	void setHostUrl(QString hostUrl);
	QString getHostUrl();
	void setSerialNumber(QString serialNumber);
	QString getSerialNumber();

	void initParams();

	void sendRequest(QString uri, HttpRequestType reqType, QString& responseBody, int& resCode, QByteArray requestByte = "");
	int queryProductsBySerialNumber(QueryProductListResult& queryResult);
	int queryAiUrl(QueryAiUrlReq& queryAiUrlReq, QueryAiUrlRes& queryRes);
	int deployAiInstance(QueryAiUrlReq& queryAiUrlReq, QueryAiUrlRes& queryRes);

	static int getConnectionTimeOut();

	/// <summary>
	/// Det+OCR Infer
	/// 
	/// post  DetPlusOcr Request
	/// </summary>
	/// <param name="url"> address of Infer</param>
	/// <param name="req">AoiReq Object,reference type</param>
	/// <param name="detPlusRsp">DetPlusRsp Object,reference type</param>
	/// <returns></returns>
	int sendInferReq_DetPlusOcr(QString url, aoi::AoiReq& req, aoi::DetPlusRsp& detPlusRsp, QString &rspJson);

	/// <summary>
	/// Det+Det Infer
	/// 
	/// post  DetPlusDet Request
	/// </summary>
	/// <param name="url"> address of Infer</param>
	/// <param name="req">AoiReq Object,reference type</param>
	/// <param name="detPlusRsp">DetPlusRsp Object,reference type</param>
	/// <returns></returns>
	int sendInferReq_DetPlusDet(QString url, aoi::AoiReq& req, aoi::DetPlusRsp& detPlusRsp, QString &rspJson);

	/// <summary>
	/// Det+Seg Infer
	/// 
	/// post  DetPlusSeg Request
	/// </summary>
	/// <param name="url"> address of Infer</param>
	/// <param name="req">AoiReq Object,reference type</param>
	/// <param name="detPlusRsp">DetPlusRsp Object,reference type</param>
	/// <returns></returns>
	int sendInferReq_DetPlusSeg(QString url, aoi::AoiReq& req, aoi::DetPlusRsp& detPlusRsp, QString &rspJson);
};
